package com.yanqu.road.dao.impl.maidian;

import com.alibaba.druid.util.JdbcUtils;
import com.yanqu.road.entity.log.maidian.LogUserBagChange;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogClickHouseDao {

    /**
     * 背包道具
     */
    public Map<Integer, List<LogUserBagChange>> selectUserBagChange(String connectUrl, long userId, long beginTime, long endTime){
        String sql = "SELECT way,isGet,goodsId,sum(num) as total,count(0) as times FROM t_l_ck_goods_dst  WHERE time >= %d AND time < %d  and userId = %d  GROUP BY way,isGet,goodsId ";
        ResultSet rs = null;
        Connection conn = null;
        Statement prepareCall = null;
        Map<Integer, List<LogUserBagChange>> map = new HashMap<>();
        try {
            conn = DriverManager.getConnection(connectUrl, null, null);
            prepareCall = conn.createStatement();
            rs = prepareCall.executeQuery(String.format(sql, beginTime, endTime, userId));
            while (rs.next()) {
                LogUserBagChange bagChange = new LogUserBagChange();
                bagChange.setWay(rs.getInt("way"));
                bagChange.setIsGet(rs.getInt("isGet"));
                bagChange.setGoodsId(rs.getInt("goodsId"));
                bagChange.setTotal(rs.getLong("total"));
                bagChange.setTimes(rs.getInt("times"));
                if(!map.containsKey(bagChange.getIsGet())){
                    map.put(bagChange.getIsGet(), new ArrayList<>());
                }
                map.get(bagChange.getIsGet()).add(bagChange);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(rs);
            JdbcUtils.close(prepareCall);
            JdbcUtils.close(conn);
        }
        return map;
    }


    /**
     * 元宝
     */
    public Map<Integer, List<LogUserBagChange>> selectUserIngotsChange(String connectUrl, long userId, long beginTime, long endTime){
        String sql = "SELECT son as way,  isGet,  sum(ingots) as total,  count(0) as times  FROM  t_l_ck_ingots_dst  WHERE 1=1  and time >= %d and time < %d  and userId = %d  GROUP BY son,isGet";
        ResultSet rs = null;
        Connection conn = null;
        Statement prepareCall = null;
        Map<Integer, List<LogUserBagChange>> map = new HashMap<>();
        try {
            conn = DriverManager.getConnection(connectUrl, null, null);
            prepareCall = conn.createStatement();
            rs = prepareCall.executeQuery(String.format(sql, beginTime, endTime, userId));
            while (rs.next()) {
                LogUserBagChange bagChange = new LogUserBagChange();
                bagChange.setWay(rs.getInt("way"));
                bagChange.setIsGet(rs.getInt("isGet"));
                bagChange.setTotal(rs.getLong("total"));
                bagChange.setTimes(rs.getInt("times"));
                if(!map.containsKey(bagChange.getIsGet())){
                    map.put(bagChange.getIsGet(), new ArrayList<>());
                }
                map.get(bagChange.getIsGet()).add(bagChange);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(rs);
            JdbcUtils.close(prepareCall);
            JdbcUtils.close(conn);
        }
        return map;
    }

}
